﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using TierService;
using OrderMgr.SysSetting;
using System.Data.SqlClient;
using TierApplication;


namespace OrderMgr.OrderInfo
{
    public partial class frmSaleReturnFeeDetail : TierService.frmTemplate
    {
        #region "Member Variables"
        public string sFuncID;
        public TierService.Utility.OperateMode OpMode;
        public string sUserID;
        public string _ReturnFeeID = "";
        public string _OrderID = "";
        public string _InvoiceID = "";
        public Utility.OrderType _OrderType;
        public Utility.OrderStatus _OrderStatus;

        T_OrderReturnFee mOrderReturnFee = new T_OrderReturnFee();

        #endregion

        public frmSaleReturnFeeDetail()
        {
            InitializeComponent();
        }

        private void frmBuyToStockDetail_Load(object sender, EventArgs e)
        {
            initForm();

            switch (OpMode)
            {
                case Utility.OperateMode.Query:
                    btnSave.Visible = false;
                    txtInvoiceID.BackColor = Color.Silver;
                    txtInvoiceID.Properties.ReadOnly = true;
                    showInfo(_ReturnFeeID);
                    break;
                case Utility.OperateMode.Insert:
                    txtReturnFeeDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    txtOrderDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    txtOrderUserID.EditValue = TierService.Utility.gUserID;
                    txtDepotID.Enabled = false;
                    txtDepotID.EditValue = ((int)Utility.DepotType.Material).ToString();
                    txtInvoiceID.Text = _InvoiceID;
                    break;
                case Utility.OperateMode.Update:
                    btnSave.Enabled = true;
                    txtInvoiceID.BackColor = Color.Silver;
                    txtInvoiceID.Properties.ReadOnly = true;
                    showInfo(_ReturnFeeID);
                    break;
                case Utility.OperateMode.Delete:
                    break;
                default:
                    break;
            }

        }

        private void initForm()
        {
            //Utility.DataToLookupComboxEx(txtOrderType, C_DMB.GetListByType(C_DMB.DType.OrderType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");
            //Utility.DataToLookupComboxEx(txtAuditUserID, (new P_User()).GetList(""), "UserID", "UserName");
            //Utility.DataToLookupComboxEx(txtInvoiceUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtInvoiceUserID, (new P_User()).GetList(""), "UserID", "UserName");

            txtOrderUserID.Enabled = false;
            switch (_OrderStatus)
            {
                case Utility.OrderStatus.S0Input:
                    btnSubmitAudit.Enabled = false;
                    break;
                case Utility.OrderStatus.S1Auditing:

                    break;
                case Utility.OrderStatus.All:
                    break;
                default:
                    break;
            }

        }


        private void showInfo(string sID)
        {
            mOrderReturnFee.GetModel(sID);
            this.txtReturnFeeID.Text = mOrderReturnFee.ReturnFeeID;
            this.txtInvoiceID.Text = mOrderReturnFee.InvoiceID;
            if (mOrderReturnFee.OrderDate != null)
                this.txtOrderDate.DateTime = mOrderReturnFee.OrderDate.Value;
            this.txtOrderUserID.EditValue = mOrderReturnFee.OrderUserID;
            this.txtDepotID.EditValue = mOrderReturnFee.DepotID;
            this.txtRemark.Text = mOrderReturnFee.Remark;

            //////////---------------Invoice
            if (mOrderReturnFee.ReturnFeeDate != null)
                this.txtReturnFeeDate.DateTime = mOrderReturnFee.ReturnFeeDate.Value;

            this.txtRemark.Text = mOrderReturnFee.Remark;
            this.txtReturnFeeMoney.Text = mOrderReturnFee.ReturnFeeMoney.Value.ToString();

            //*****************Order Detail*****************//
            BindOrderDetail();

        }
        private void GetFormReturValue(string str)
        {
            string sID = str;
            if (sID != "")
            {
                showInfo(sID);
            }
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            //if (txtInvoiceMoney.Value - txtReturnFeedMoney.Value < txtReturnFeeMoney.Value)
            //{
            //    XtraMessageBox.Show("本次回款金额不能大于“开票总金额”-“已回款金额”!");
            //    return;
            //}
            if (txtReturnFeeDate.Text == "")
            {
                XtraMessageBox.Show("开票日期不能为空!");
                return;
            }
            if (SaveOrder())
            {
                XtraMessageBox.Show("销售回款单保存成功!");
                DialogResult = DialogResult.OK;
            }
        }

        private bool SaveOrder()
        {
            if ((txtInvoiceID.Text == ""))
            {
                XtraMessageBox.Show("请选择开票编号!");
                txtInvoiceID.Focus();
                return false;
            }

            mOrderReturnFee.ReturnFeeID = this.txtReturnFeeID.Text;
            mOrderReturnFee.InvoiceID = this.txtInvoiceID.Text;

            mOrderReturnFee.OrderDate = DateTime.Parse(this.txtOrderDate.Text);
            mOrderReturnFee.OrderUserID = (string)this.txtOrderUserID.EditValue;
            mOrderReturnFee.OrderType = "13";
            mOrderReturnFee.OrderStatus = "0";
            mOrderReturnFee.DepotID = (string)this.txtDepotID.EditValue;

            mOrderReturnFee.Sign = 0;

            mOrderReturnFee.ReturnFeeMoney = this.txtReturnFeeMoney.Value;
            mOrderReturnFee.ReturnFeeDate = this.txtReturnFeeDate.DateTime;
            mOrderReturnFee.Remark = this.txtRemark.Text;

            bool flag = false;
            switch (OpMode)
            {
                case TierService.Utility.OperateMode.Insert:
                    try
                    {
                        mOrderReturnFee.OrderID = _OrderID;
                        mOrderReturnFee.ReturnFeeID = Utility.GetSeqval(Utility.SeqName.PayFeeID.ToString());
                        mOrderReturnFee.Add();
                        _ReturnFeeID = mOrderReturnFee.ReturnFeeID;

                        txtReturnFeeID.Text = _ReturnFeeID;
                        BindOrderDetail();
                        OpMode = Utility.OperateMode.Update;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("新增出错,错误原因为:" + ex.Message);
                        break;
                    }
                    //MessageBox.Show("入库单新增成功!");
                    btnSubmitAudit.Enabled = true;
                    flag = true;
                    //DialogResult = DialogResult.OK;
                    break;
                case TierService.Utility.OperateMode.Update:
                    try
                    {
                        // mCustomer.CustID = txtCustID.Text;
                        mOrderReturnFee.Update();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("入库单信息修改出错,错误原因为:" + ex.Message);
                        break;
                    }


                    btnSubmitAudit.Enabled = true;
                    //MessageBox.Show("入库单信息修改成功!");
                    //DialogResult = DialogResult.OK;
                    flag = true;
                    break;
                default:
                    break;
            }
            return flag;
        }

        private void btnProductAdd_Click(object sender, EventArgs e)
        {
            if (txtReturnFeeID.Text == "")
            {
                if (TierService.Utility.Msg_Question("此销售出库单未生成，是否要生成出库单?") == DialogResult.OK)
                {
                    if (!SaveOrder())
                        return;
                }
            }

            if (gvSaleProduct.RowCount < 1) return;
            DataRow dr = gvSaleProduct.GetDataRow(gvSaleProduct.FocusedRowHandle);
            string sProductID = dr["ProductID"].ToString();
            frmSaleShipStockProductDetail mDetail = new frmSaleShipStockProductDetail();
            //mDetail._POrderDetailID = sOrderDetailID;
            mDetail._POrderID = txtInvoiceID.Text;
            mDetail._OrderID = txtReturnFeeID.Text;
            mDetail._ProductID = sProductID;
            mDetail.OpMode = Utility.OperateMode.Insert;
            if (mDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BindOrderDetail();


            }

        }


        private void BindOrderDetail()
        {

            if (_ReturnFeeID == "") return;

            //BindSaleShipByOrderID(txtInvoiceID.Text);
           
        }


        private void txtPOrderID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmSaleReturnFeeNeed fDetail = new frmSaleReturnFeeNeed();
            fDetail.gFuncType = Utility.FuncType.Select;
            fDetail._IsStock = "Y";
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtInvoiceID.Text = fDetail._OrderID;
                string sInvoceID = txtInvoiceID.Text;
                //生成商品明细

                //T_OrderInvoice mOrderInvoice = new T_OrderInvoice();
                //mOrderInvoice.GetModel(sInvoceID);
                //BindSaleShipByOrderID(mOrderInvoice.OrderID);
               
            }
        }

        private void txtPOrderID_EditValueChanged(object sender, EventArgs e)
        {
            string sInvoiceID = txtInvoiceID.Text;
            if (sInvoiceID != "")
            {
                T_OrderInvoice mOrderInvoice = new T_OrderInvoice();
                mOrderInvoice.GetModel(sInvoiceID);
                txtInvoiceUserID.EditValue = mOrderInvoice.OrderUserID;
                if (mOrderInvoice.OrderDate != null)
                    txtInvoiceDate.DateTime = mOrderInvoice.OrderDate.Value;
                txtInvoiceMoney.Value = mOrderInvoice.InvoiceMoney.Value;
                txtInvoiceNo.Text = mOrderInvoice.InvoiceNo;

                string sSaleOrderID = mOrderInvoice.OrderID;
                T_Order mOrder = new T_Order();
                mOrder.GetModel(sSaleOrderID);
                _OrderID = sSaleOrderID;
                T_Customer mCustomer = new T_Customer();
                mCustomer.GetModel(mOrder.CustID);
                txtSaleInvoiceMoney.Value = mOrder.PayFee.Value;
                txtCustName.Text = mCustomer.CustName;
                BindSaleShipByOrderID(sSaleOrderID);


                StringBuilder strSql = new StringBuilder();
                strSql.Append("Select InvoiceMoney,(select isnull(Sum(ReturnFeeMoney),0) from T_OrderReturnFee where InvoiceID=a.InvoiceID and ReturnFeeID<>'"+txtReturnFeeID.Text+"') ReturnFeeMoney");
                strSql.Append(" FROM T_OrderInvoice a ");
                strSql.Append(" where a.InvoiceID='" + sInvoiceID + "'");
                DataSet dsInvoice = DBUtility.DbHelperSQL.Query(strSql.ToString());
                //txtSaleInvoiceMoney.Text = dsInvoice.Tables[0].Rows[0]["InvoiceMoney"].ToString();
                txtReturnFeedMoney.Text = dsInvoice.Tables[0].Rows[0]["ReturnFeeMoney"].ToString();
            }

        }

        private void BindSaleShipByOrderID(string sSaleOrderID)
        {
            DataTable dtOrderDetail = T_Order.GetSaleShipByOrderID(sSaleOrderID).Tables[0];
            gcSaleProduct.DataSource = dtOrderDetail;

            BindOrderReturnFeeByInvoiceID();
   
        }

        private void BindOrderReturnFeeByInvoiceID()
        {
            //T_OrderReturnFee mOrderReturnFee = new T_OrderReturnFee();
            //DataTable dtOrderReturnFee = mOrderReturnFee.GetSaleReturnFeeByInvoiceID(txtInvoiceID.Text).Tables[0];
            //gcOrderReturnFee.DataSource = dtOrderReturnFee;

        }

        private void btnAudit_Click(object sender, EventArgs e)
        {

        }

        private void btnOrderPrint_Click(object sender, EventArgs e)
        {
            string sOrderID = txtReturnFeeID.Text;
            if (sOrderID != "")
            {
                TdsOrder dsOrder = TOrderFunc.GetOrderPrintList(sOrderID);
                RptOrderBuyToStock xrptOrderBuy = new RptOrderBuyToStock();
                xrptOrderBuy.DataSource = dsOrder;
                xrptOrderBuy.ShowPreviewDialog();
            }
        }

        private void btnSubmitAudit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确认此单据开票信息，确认后将不能更改？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            T_Order mOrder = new T_Order();
            mOrder.Audit(txtReturnFeeID.Text, Utility.gUserID,0);
            Close();
        }
    }
}